package cn.klaw.config;

import cn.hutool.core.net.url.UrlBuilder;
import cn.klaw.constant.ConfigConst;
import cn.klaw.constant.TableFieldsNameConst;
import cn.klaw.util.StringUtil;
import cn.klaw.util.XSSEscape;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 *
 * @version V3.1.0
 *
 * @date 2021/3/16 8:47
 */
@Data
public class ConfigValueUtil {

    public static final String PREFIX = "config";

    @Value("spring.application.name:KLAW")
    private String applicationName;

//    /**
//     * 环境路径
//     */
//    @Value("${config.Path:}")
//    private String path;
    /**
     * 数据库备份文件路径
     */
    private String dataBackupFilePath;
    /**
     * 临时文件存储路径
     */
    private String temporaryFilePath;
    /**
     * 系统文件存储路径
     */
    private String systemFilePath;
    /**
     * 文件模板存储路径
     */
    private String templateFilePath;
    /**
     * 代码模板存储路径
     */
    private String templateCodePath;
    /**
     * vue3代码模板存储路径
     */
    private String templateCodePathVue3;
    /**
     * 邮件文件存储路径
     */
    private String emailFilePath;
    /**
     * 大屏图片存储目录
     */
    private String biVisualPath;
    /**
     * 文档管理存储路径
     */
    private String documentFilePath;
    /**
     * 文件在线预览存储pdf
     */
    private String documentPreviewPath;
    /**
     * 用户头像存储路径
     */
    private String userAvatarFilePath;
    /**
     * IM聊天图片+语音存储路径
     */
    private String imContentFilePath;
    /**
     * 允许上传文件类型
     */
    private String allowUploadFileType;
    /**
     * 允许图片类型
     */
    private String allowUploadImageType;

    /**
     * 允许预览类型
     */
    private String allowPreviewFileType;

    /**
     * 预览方式
     */
    private String previewType;

    /**
     * 预览方式
     */
    private String kkFileUrl;

    /**
     * 多语言json路径
     */
    private String baseLanguagePath;

    /**
     * 前端文件目录
     */
    private String serviceDirectoryPath = ConfigConst.CODE_TEMP_FOLDER;
    /**
     * 代码生成器命名空间
     */
    private String codeAreasName;

    /**
     * 前端附件文件目录
     */
    private String webAnnexFilePath;

    /**
     * ApacheShardingSphere 配置开关
     */
    private boolean shardingSphereEnabled;

    /**
     * 软件的错误报告
     */
    private String errorReport;
    /**
     * 软件的错误报告发给谁
     */
    private String errorReportTo;
    /**
     * 系统日志启用：true、false
     */
    private String recordLog;
    /**
     * 多租户启用：true、false
     */
    private boolean multiTenancy;
    /**
     * 多租户接口
     */
    private String multiTenancyUrl;
    /**
     * 多租户字段
     */
    private String multiTenantColumn = TableFieldsNameConst.F_TENANT_ID;
    /**
     * 字段多租户忽略表
     */
    private List<String> multiTenantIgnoreTable = initMultiTenantIgnoreTable();
    /**
     * 多租户登录短信验证接口
     */
    private String multiTenancyOfficialLoginCodeUrl = null;
    /**
     * 多租户重置密码短信验证接口
     */
    private String multiTenancyOfficialResetCodeUrl = null;
    /**
     * 开启逻辑删除功能
     */
    private boolean enableLogicDelete = false;
    /**
     * 扫码登录过期时间
     */
    private Long codeCertificateTimeout = 180L;
    /**
     * 逻辑删除字段
     */
    private String logicDeleteColumn = TableFieldsNameConst.F_DELETE_MARK;
    /**
     * 版本
     */
    private String softVersion;
    /**
     * 推送是否启动：false、true
     */
    private String igexinEnabled;
    /**
     * APPID
     */
    private String igexinAppid;
    /**
     * APPKEY
     */
    private String igexinAppkey;
    /**
     * MASTERSECRET
     */
    private String igexinMastersecret;

    private String appUpdateContent;
    private String appVersion;

    /**
     * pc端服务器域名
     */
    private String frontDomain;

    /**
     * app端服务器域名
     */
    private String appDomain;

    /**
     * 服务器域名
     */
    private String apiDomain;


    /**
     * 工作流域名
     */
    private String flowDomain;

    /**
     * 检查PDF文件安全
     */
    private boolean checkFilePdf;


    /**
     * -------------跨域配置-----------
     */
//    @Value("${config.Origins}")
//    private String origins;
//    @Value("${config.Methods}")
//    private String methods;

    /**
     * -------------是否开启测试环境，admin账户可以无限登陆，并且无法修改密码-----------
     */
    private String testVersion;


    /**
     * -------------uniPush在线-----------
     */
    private String appPushUrl;

    public String getMultiTenancyOfficialLoginCodeUrl() {
        if(multiTenancyOfficialLoginCodeUrl == null){
            if(StringUtil.isNotEmpty(multiTenancyUrl)){
                UrlBuilder urlBuilder1 = UrlBuilder.of(multiTenancyUrl);
                multiTenancyOfficialLoginCodeUrl = String.format("%s://%s:%s/api/Saas/Tenant/LoginSmsCodeCheck/", urlBuilder1.getSchemeWithDefault(), urlBuilder1.getHost(), urlBuilder1.getPortWithDefault());
            }else{
                multiTenancyOfficialLoginCodeUrl = "";
            }
        }
        return multiTenancyOfficialLoginCodeUrl;
    }

    public String getMultiTenancyOfficialResetCodeUrl() {
        if(multiTenancyOfficialResetCodeUrl == null){
            if(StringUtil.isNotEmpty(multiTenancyUrl)){
                UrlBuilder urlBuilder1 = UrlBuilder.of(multiTenancyUrl);
                multiTenancyOfficialResetCodeUrl = String.format("%s://%s:%s/api/Saas/Tenant/ResetPasswordSmsCodeCheck/", urlBuilder1.getSchemeWithDefault(), urlBuilder1.getHost(), urlBuilder1.getPortWithDefault());
            }else{
                multiTenancyOfficialResetCodeUrl = "";
            }
        }
        return multiTenancyOfficialResetCodeUrl;
    }



    public String getServiceDirectoryPath(){
        String folder = StringUtil.isNotEmpty(serviceDirectoryPath) ? serviceDirectoryPath : ConfigConst.CODE_TEMP_FOLDER;
        return getXssPath(folder +"/");
    }

    public String getDataBackupFilePath() {
        String folder = StringUtil.isNotEmpty(dataBackupFilePath) ? dataBackupFilePath : ConfigConst.DATA_BACKUP_FOLDER;
        return getXssPath(folder +"/");
    }

    public String getTemporaryFilePath() {
        String folder = StringUtil.isNotEmpty(temporaryFilePath) ? temporaryFilePath : ConfigConst.TEMPORARY_FOLDER;
        return getXssPath(folder +"/");
    }

    public String getSystemFilePath() {
        String folder = StringUtil.isNotEmpty(systemFilePath) ? systemFilePath : ConfigConst.SYSTEM_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getTemplateFilePath() {
        String folder = StringUtil.isNotEmpty(templateFilePath) ? templateFilePath : ConfigConst.TEMPLATE_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getTemplateCodePath() {
        String folder = StringUtil.isNotEmpty(templateCodePath) ? templateCodePath : ConfigConst.TEMPLATE_CODE_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getTemplateCodePathVue3() {
        String folder = StringUtil.isNotEmpty(templateCodePathVue3) ? templateCodePathVue3 : ConfigConst.TEMPLATEVUE3_CODE_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getEmailFilePath() {
        String folder = StringUtil.isNotEmpty(emailFilePath) ? emailFilePath : ConfigConst.EMAIL_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getDocumentPreviewPath() {
        String folder = StringUtil.isNotEmpty(documentPreviewPath) ? documentPreviewPath : ConfigConst.DOCUMENT_PREVIEW_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getUserAvatarFilePath() {
        String folder = StringUtil.isNotEmpty(userAvatarFilePath) ? userAvatarFilePath : ConfigConst.USER_AVATAR_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getImContentFilePath() {
        String folder = StringUtil.isNotEmpty(imContentFilePath) ? imContentFilePath : ConfigConst.IM_CONTENT_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getDocumentFilePath() {
        String folder = StringUtil.isNotEmpty(documentFilePath) ? documentFilePath : ConfigConst.DOCUMENT_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getWebAnnexFilePath() {
        String folder = StringUtil.isNotEmpty(webAnnexFilePath) ? webAnnexFilePath : ConfigConst.WEB_ANNEX_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getBiVisualPath() {
        String folder = StringUtil.isNotEmpty(biVisualPath) ? biVisualPath : ConfigConst.BI_VISUAL_FOLDER;
        return getXssPath(folder + "/");
    }

    public String getBaseLanguagePath() {
        String folder = StringUtil.isNotEmpty(baseLanguagePath) ? baseLanguagePath : ConfigConst.BASE_LANGUAGE;
        return getXssPath(folder + "/");
    }

    private String getXssPath(String path){
        String xssPath = XSSEscape.escapePath(path);
        return xssPath;
    }

    public void setMultiTenantIgnoreTable(List<String> multiTenantIgnoreTable) {
        initMultiTenantIgnoreTable();
        this.multiTenantIgnoreTable.addAll(multiTenantIgnoreTable);
        // 全部转小写, 对比时也转小写
        this.multiTenantIgnoreTable = this.multiTenantIgnoreTable.stream().map(String::toLowerCase).collect(Collectors.toList());
    }

    public List<String> getMultiTenantIgnoreTable() {
        return new ArrayList<>(this.multiTenantIgnoreTable);
    }

    private List<String> initMultiTenantIgnoreTable(){
        List<String> multiTenantIgnoreTable = this.multiTenantIgnoreTable;
        if(multiTenantIgnoreTable == null){
            multiTenantIgnoreTable = new ArrayList<>();
        }else{
            multiTenantIgnoreTable.clear();
        }
        multiTenantIgnoreTable.add("dual");
        return multiTenantIgnoreTable;
    }

}
